package cz.proks.easyhelpdesk.gwt.activity;

import java.util.Collection;
import java.util.Date;
import java.util.LinkedList;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceHistoryMapper;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

import cz.proks.easyhelpdesk.api.model.Helpdesk;
import cz.proks.easyhelpdesk.api.model.HelpdeskMember;
import cz.proks.easyhelpdesk.api.model.HelpdeskRole;
import cz.proks.easyhelpdesk.api.model.base.Id;
import cz.proks.easyhelpdesk.gwt.ClientFactory;
import cz.proks.easyhelpdesk.gwt.Globals;
import cz.proks.easyhelpdesk.gwt.activity.base.RestrictedActivity;
import cz.proks.easyhelpdesk.gwt.place.HelpdesksPlace;
import cz.proks.easyhelpdesk.gwt.place.NewHelpdeskPlace;
import cz.proks.easyhelpdesk.gwt.restapi.HelpdeskMemberRestApi;
import cz.proks.easyhelpdesk.gwt.restapi.HelpdeskRestApi;
import cz.proks.easyhelpdesk.gwt.restapi.RestIdCallback;
import cz.proks.easyhelpdesk.gwt.ui.NewHelpdeskView;
import cz.proks.easyhelpdesk.gwt.ui.NewHelpdeskView.Presenter;
import cz.proks.easyhelpdesk.gwt.ui.base.RestrictedView;

public class NewHelpdeskActivity extends RestrictedActivity<NewHelpdeskPlace> implements Presenter{
    private NewHelpdeskView view;
    
    public NewHelpdeskActivity(NewHelpdeskPlace place, ClientFactory clientFactory, PlaceHistoryMapper placeMapper) {
        super(place, clientFactory, placeMapper);
        this.view = getClientFactory().getCreateHelpdeskView();
    }

    @Override
    protected RestrictedView getView() {
        return view;
    }

    @Override
    protected void onPermission(AcceptsOneWidget panel, EventBus eventBus) {
        view.onErase();        
        view.setPresenter(this);
        panel.setWidget(view);        
    }
    
    @Override
    public void saveHelpdesk(final Helpdesk helpdesk) {                
        completeHelpdesk(helpdesk);
        
        if (!isHelpdeskOkBeforeSave(helpdesk))
            return;
        
        new HelpdeskRestApi().create(helpdesk, new RestIdCallback() {
            
            @Override
            public void onIdSuccess(Id id) {
                helpdesk.setId(id.getId());                                
                Globals.addHelpdesk(helpdesk);
                
                final HelpdeskMember helpdeskMember = getDefaultHelpdeskMember(helpdesk);
                
                new HelpdeskMemberRestApi().create(helpdeskMember, new RestIdCallback() {
                    
                    @Override
                    public void onIdSuccess(Id id) {
                        helpdeskMember.setId(id.getId());
                        Globals.getHelpdeskMemberMapFor(helpdesk).put(id.getId(), helpdeskMember);
                        
                        getClientFactory().getPlaceController().goTo(new HelpdesksPlace());
                    }
                });
                                
            }
        });        
    }
    
    @Override
    public void cancel() {
        getClientFactory().getPlaceController().goTo(new HelpdesksPlace());
    }
   
    private void completeHelpdesk(Helpdesk helpdesk) {                
        helpdesk.setCreated(new Date());        
        helpdesk.setOwnerId(Globals.getCurrentUser().getId());               
    }   
    
    private HelpdeskMember getDefaultHelpdeskMember(Helpdesk helpdesk) {
        HelpdeskMember helpdeskMember = new HelpdeskMember();
        
        helpdeskMember.setHelpdeskId(helpdesk.getId());
        helpdeskMember.setUserId(Globals.getCurrentUser().getId());
        helpdeskMember.setHelpdeskRoles(new LinkedList<HelpdeskRole>(getDefaultHelpdeskRoles()));
        
        return helpdeskMember;
    }
    
    private Collection<HelpdeskRole> getDefaultHelpdeskRoles() {
        Collection<HelpdeskRole> roles = new LinkedList<HelpdeskRole>();
        
        roles.add(HelpdeskRole.OPERATOR);
        
        return roles;
    }
    
    private boolean isHelpdeskOkBeforeSave(Helpdesk helpdesk) {
        if (helpdesk.getName() == null || helpdesk.getName().isEmpty()) {
            Window.alert(getClientFactory().getHelpdeskMessages().errorHelpdedskNameEmpty());
            return false;
        }
        
        return true;
    }
}
